package org.csu.mypetstore.controller;
import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.ManageCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/managecatalog")
@SessionAttributes("allCategoryList")

public class ManageCatalogController {
    @Autowired
    private ManageCategoryService manageCategoryService;
    @Autowired
    private CatalogService catalogService;


//    @GetMapping("/viewDeleteCategory")
//    public String viewDeleteCategory(String CategoryId, Model model) {
//        manageCategoryService.deleteCategory(CategoryId);
//        List<Product> productList = catalogService.getProductListByCategory(CategoryId);
//        for(Product product : productList) {
//            manageCategoryService.deleteProduct(product.getProductId());
//        }
//        List<Item> ItemList = catalogService.getAllItem();
//        List<Product> allproductList = catalogService.getAllProduct();
//        List<Category> categoryList = catalogService.getAllCategoryList();
//        model.addAttribute("categoryList", categoryList);
//        model.addAttribute("productList", allproductList);
//        model.addAttribute("itemList", ItemList);
//        return "/manage/productManage";
//    }
    @PostMapping("/viewDeleteCategory")
    public CommonResponse<Object> viewDeleteCategory(@RequestParam("CategoryId") String CategoryId) {
        manageCategoryService.deleteCategory(CategoryId);
        List<Product> productList = catalogService.getProductListByCategory(CategoryId);
        for(Product product : productList) {
            manageCategoryService.deleteProduct(product.getProductId());
        }
        List<Item> ItemList = catalogService.getAllItem();
        List<Product> allproductList = catalogService.getAllProduct();
        List<Category> categoryList = catalogService.getAllCategoryList();

        return CommonResponse.createForSuccess(categoryList,allproductList,ItemList);
    }

//    @PostMapping("/viewUpdateProduct")
//    public String viewUpdateProduct(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException{
//        String categoryId = request.getParameter("categoryId");
//        String productId = request.getParameter("productId");
//        String name = request.getParameter("name");
//        String description = request.getParameter("description");
//        String image = request.getParameter("file1");
//        if(image.equals("file1")){
//            Product product = catalogService.getProduct(productId);
//            image = product.getImage();
//        }
//        manageCategoryService.updateProduct(productId,categoryId,name,description,image);
//        List<Item> ItemList = catalogService.getAllItem();
//        List<Product> allproductList = catalogService.getAllProduct();
//        List<Category> categoryList = catalogService.getAllCategoryList();
//        model.addAttribute("categoryList", categoryList);
//        model.addAttribute("productList", allproductList);
//        model.addAttribute("itemList", ItemList);
//        return "/manage/productManage";
//    }
    @PostMapping("/viewUpdateProduct")
    public CommonResponse<Object> viewUpdateProduct(@RequestParam("categoryID") String categoryId,
                                                    @RequestParam("productId") String productId,
                                                    @RequestParam("name") String name,
                                                    @RequestParam("description") String description,
                                                    @RequestParam("image") String image){
        if(image.equals("file1")){
            Product product = catalogService.getProduct(productId);
            image = product.getImage();
        }
        manageCategoryService.updateProduct(productId,categoryId,name,description,image);
        List<Item> ItemList = catalogService.getAllItem();
        List<Product> allproductList = catalogService.getAllProduct();
        List<Category> categoryList = catalogService.getAllCategoryList();
        return CommonResponse.createForSuccess(categoryList,allproductList,ItemList);
    }

//    @PostMapping("/viewUpdateCategory")
//    public String viewUpdateCategory(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException{
//        String categoryId = request.getParameter("categoryId");
//        String name = request.getParameter("name");
//        String description = request.getParameter("file1");
//        if(description.equals("file1")){
//            Category category = catalogService.getCategory(categoryId);
//            description = category.getDescription();
//        }
//        manageCategoryService.updateCategory(categoryId,name,description);
//        List<Item> ItemList = catalogService.getAllItem();
//        List<Product> allproductList = catalogService.getAllProduct();
//        List<Category> categoryList = catalogService.getAllCategoryList();
//        model.addAttribute("categoryList", categoryList);
//        model.addAttribute("productList", allproductList);
//        model.addAttribute("itemList", ItemList);
//        return "/manage/productManage";
//    }
    @PostMapping("/viewUpdateCategory")
    public CommonResponse<Object> viewUpdateCategory(@RequestParam("categoryId") String categoryId,
                                                     @RequestParam("name") String name,
                                                     @RequestParam("description") String description){
        if(description.equals("file1")){
            Category category = catalogService.getCategory(categoryId);
            description = category.getDescription();
        }
        manageCategoryService.updateCategory(categoryId,name,description);
        List<Item> ItemList = catalogService.getAllItem();
        List<Product> allproductList = catalogService.getAllProduct();
        List<Category> categoryList = catalogService.getAllCategoryList();
        return CommonResponse.createForSuccess(categoryList,allproductList,ItemList);
    }

//    @PostMapping("/viewInsetProduct")
//    public String viewInsetProduct(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException{
//        String categoryId = request.getParameter("categoryId");
//        String productId = request.getParameter("productId");
//        String name = request.getParameter("name");
//        String description = request.getParameter("description");
//        String image = request.getParameter("file1");
//        if(image.equals("file1")){
//            Product product = catalogService.getProduct(productId);
//            image = product.getImage();
//        }
//        manageCategoryService.insertProduct(productId,categoryId,name,description,image);
//        List<Item> ItemList = catalogService.getAllItem();
//        List<Product> allproductList = catalogService.getAllProduct();
//        List<Category> categoryList = catalogService.getAllCategoryList();
//        model.addAttribute("categoryList", categoryList);
//        model.addAttribute("productList", allproductList);
//        model.addAttribute("itemList", ItemList);
//        return "/manage/productManage";
//    }
    @PostMapping("/viewInsetProduct")
    public CommonResponse<Object> viewInsetProduct(@RequestParam("categoryId") String categoryId,
                                                   @RequestParam("productId") String productId,
                                                   @RequestParam("name") String name,
                                                   @RequestParam("description") String description,
                                                   @RequestParam("image") String image){

        if(image.equals("file1")){
            Product product = catalogService.getProduct(productId);
            image = product.getImage();
        }
        manageCategoryService.insertProduct(productId,categoryId,name,description,image);
        List<Item> ItemList = catalogService.getAllItem();
        List<Product> allproductList = catalogService.getAllProduct();
        List<Category> categoryList = catalogService.getAllCategoryList();
        return CommonResponse.createForSuccess(categoryList,allproductList,ItemList);
    }

//    @PostMapping("/viewInsetCategory")
//    public String viewInsetCategory(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException{
//        String categoryId = request.getParameter("categoryId");
//        String name = request.getParameter("name");
//        String description = request.getParameter("file1");
//        if(description.equals("file1")){
//            Category category = catalogService.getCategory(categoryId);
//            description = category.getDescription();
//        }
//        manageCategoryService.insertCategory(categoryId,name,description);
//        List<Item> ItemList = catalogService.getAllItem();
//        List<Product> allproductList = catalogService.getAllProduct();
//        List<Category> categoryList = catalogService.getAllCategoryList();
//        model.addAttribute("categoryList", categoryList);
//        model.addAttribute("productList", allproductList);
//        model.addAttribute("itemList", ItemList);
//        return "/manage/productManage";
//
//    }
    @PostMapping("/viewInsetCategory")
    public CommonResponse<Object> viewInsetCategory(@RequestParam("categoryId") String categoryId,
                                                    @RequestParam("name") String name,
                                                    @RequestParam("description") String description){
        if(description.equals("file1")){
            Category category = catalogService.getCategory(categoryId);
            description = category.getDescription();
        }
        manageCategoryService.insertCategory(categoryId,name,description);
        List<Item> ItemList = catalogService.getAllItem();
        List<Product> allproductList = catalogService.getAllProduct();
        List<Category> categoryList = catalogService.getAllCategoryList();
        return CommonResponse.createForSuccess(categoryList,allproductList,ItemList);

    }


    @PostMapping("/viewDeleteProduct")
    public CommonResponse<Object> viewDeleteProduct(@RequestParam("ProductId") String ProductId) {
        manageCategoryService.deleteProduct(ProductId);
        List<Item> ItemList = catalogService.getAllItem();
        List<Product> allproductList = catalogService.getAllProduct();
        List<Category> categoryList = catalogService.getAllCategoryList();
        return CommonResponse.createForSuccess(categoryList,allproductList,ItemList);
    }


    @PostMapping("/viewDeleteItem")
    public CommonResponse<Object> viewDeleteItem(@RequestParam("ItemId") String ItemId) {
        manageCategoryService.deleteItem(ItemId);
        List<Item> ItemList = catalogService.getAllItem();
        List<Product> allproductList = catalogService.getAllProduct();
        List<Category> categoryList = catalogService.getAllCategoryList();
        return CommonResponse.createForSuccess(categoryList,allproductList,ItemList);
    }

    @PostMapping("/viewUpdateItem")
    public CommonResponse<Object> viewUpdateItem(@RequestParam("itemId") String itemId,
                                                 @RequestParam("productId") String productId,
                                                 @RequestParam("attribute1")String attribute1,
                                                 @RequestParam("listPrice") String listPrice,
                                                 @RequestParam("unitCost") String unitCost){

        BigDecimal listPrice1 = new BigDecimal(listPrice);
        BigDecimal unitCost1 = new BigDecimal(unitCost);

        manageCategoryService.updateItem(itemId, productId, listPrice1, unitCost1, 1, "p", attribute1, null, null, null, null);
        List<Item> itemList = catalogService.getAllItem();
        return CommonResponse.createForSuccess(itemList);
    }

//    @PostMapping("/viewInsertItem")
//    public String viewInsertItem(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
//        String itemId = request.getParameter("itemId");
//        String productId = request.getParameter("productId");
//        String quantity = request.getParameter("quantity");
//        BigDecimal listPrice = new BigDecimal(request.getParameter("listPrice"));
//        BigDecimal unitCost = new BigDecimal(request.getParameter("unitCost"));
//        String attribute1 = request.getParameter("attribute1");
//        manageCategoryService.insertItem(itemId, productId, listPrice, unitCost, 1, "p", attribute1, null, null, null, null);
//        List<Item> itemList = catalogService.getAllItem();
//        model.addAttribute("itemList", itemList);
//        return "/manage/productManage";
//    }
    @PostMapping("/viewInsertItem")
    public CommonResponse<Object> viewInsertItem(@RequestParam("itemId") String itemId,
                                                 @RequestParam("productId") String productId,
                                                 @RequestParam("attribute1")String attribute1,
                                                 @RequestParam("listPrice") String listPrice,
                                                 @RequestParam("unitCost") String unitCost){

        BigDecimal listPrice1 = new BigDecimal(listPrice);
        BigDecimal unitCost1 = new BigDecimal(unitCost);

        manageCategoryService.insertItem(itemId, productId, listPrice1, unitCost1, 1, "p", attribute1, null, null, null, null);
        List<Item> itemList = catalogService.getAllItem();
        return CommonResponse.createForSuccess(itemList);
    }

}






